package scu.maqiang.mesh;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import scu.maqiang.numeric.Constants;
import scu.maqiang.numeric.ParamCheck;

public class Tecplot {
	public static String ZoneString(Mesh mesh) {
		return "ZONE NODES=" + mesh.getNv() + ", ELEMENTS=" + mesh.getTecplotNt() + ", DATAPACKING=POINT, ZONETYPE=" + mesh.getTecplotType();
	}
	
//	public static String ZoneString(MeshTemplate<? extends Node, ? extends Element, ? extends Element> mesh) {
//		return "ZONE NODES=" + mesh.getNv() + ", ELEMENTS=" + mesh.getTecplotNt() + ", DATAPACKING=POINT, ZONETYPE=" + mesh.getTecplotType();
//	}

	public static String ZoneStringTime(Mesh mesh) {
		return ZoneString(mesh) + ", SOLUTIONTIME = ";
	}
	
//	public static String ZoneStringTime(MeshTemplate<? extends Node, ? extends Element, ? extends Element> mesh) {
//		return ZoneString(mesh) + ", SOLUTIONTIME = ";
//	}

	public static String ZoneStringShareTime(Mesh mesh) {
		int nDim = mesh.getnDim();
		if (nDim == 1) {
			return ZoneString(mesh) + ", VARSHARELIST=([1] = 1), CONNECTIVITYSHAREZONE = 1, SOLUTIONTIME = ";
		}else if (nDim == 2) {
			return ZoneString(mesh) + ", VARSHARELIST=([1, 2] = 1), CONNECTIVITYSHAREZONE = 1, SOLUTIONTIME = ";
		}else if(nDim == 3) {
			return ZoneString(mesh) + ", VARSHARELIST=([1, 2, 3] = 1), CONNECTIVITYSHAREZONE = 1, SOLUTIONTIME = ";
		}
		return null;
	}
	
	public static String ZoneStringDeformShareTime(Mesh mesh) {
		int nDim = mesh.getnDim();
		if (nDim == 1) {
			return ZoneString(mesh) + ", CONNECTIVITYSHAREZONE = 1, SOLUTIONTIME = ";
		}else if (nDim == 2) {
			return ZoneString(mesh) + ", CONNECTIVITYSHAREZONE = 1, SOLUTIONTIME = ";
		}else if(nDim == 3) {
			return ZoneString(mesh) + ", CONNECTIVITYSHAREZONE = 1, SOLUTIONTIME = ";
		}
		return null;
	}	
	
//	public static String ZoneStringShareTime(MeshTemplate<? extends Node, ? extends Element, ? extends Element> mesh) {
//		int nDim = mesh.getnDim();
//		if (nDim == 1) {
//			return ZoneString(mesh) + "I = " + mesh.getNv() + ", SOLUTIONTIME = ";
//		}else if (nDim == 2) {
//			return ZoneString(mesh) + ", VARSHARELIST=([1, 2] = 1), CONNECTIVITYSHAREZONE = 1, SOLUTIONTIME = ";
//		}else if(nDim == 3) {
//			return ZoneString(mesh) + ", VARSHARELIST=([1, 2, 3] = 1), CONNECTIVITYSHAREZONE = 1, SOLUTIONTIME = ";
//		}
//		return null;
//	}
	
	public static String ZoneString(Mesh2T6 mesh) {
		return "ZONE NODES=" + mesh.getNv() + ", ELEMENTS=" + 4 * mesh.getNt() + ", DATAPACKING=POINT, ZONETYPE=FETRIANGLE";
	}
	
	public static String ZoneString(Mesh1L3 mesh) {
		return "ZONE NODES=" + mesh.getNv() + ", ELEMENTS=" + 2 * mesh.getNt() + ", DATAPACKING=POINT, ZONETYPE=FELINESEG";
	}
	
	public static String[] dimVariableString = new String[]{"variables = \"x\"", 
			                                                "variables = \"x\", \"y\"",
			                                                "variables = \"x\", \"y\", \"z\""};
	
	public static String meshFileString = "title = mesh file";
	public static String solutionFileString = "title = mesh solution file";
	
	public static void LineXY(String fileName, double[] a, double[][] xx) {
		int n = xx.length;
		int m = xx[0].length;
		FileWriter fw = null;
		try {
			fw = new FileWriter(fileName);
			for(int i = 0; i < m; i++) {
				fw.write(a[i] + "\t");
				for(int j = 0; j < n; j++) {
					fw.write(xx[j][i] + "\t");
				}
				fw.write("\n");
			}
			fw.flush();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void LineXY(String fileName, ArrayList<double[]> xx) {
		FileWriter fw = null;
		int k = 1;
		try {
			fw = new FileWriter(fileName);
			for(double[] data : xx) {
				fw.write(k++ + "\t ");
				for(int i = 0, n = data.length; i < n; i++) {
					fw.write(data[i] + "\t ");
				}
				fw.write("\n");
			}
			fw.flush();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void lineXYTime(String fileName, double[] x, double[] y, double time) {
		int N = x.length;
		ParamCheck.checkEqual(N, y.length);
		if(Math.abs(time) < Constants.Er) {
			try(FileWriter fw = new FileWriter(fileName)) {
				fw.write("variables = \"x\", \"y\"\n");
				fw.write("ZONE DATAPACKING=POINT, I = " + N + ", SOLUTIONTIME = 0.0\n");
				for(int i = 0; i < N; i++) {
					fw.write(x[i] + "\t" + y[i] + "\n");
				}
				fw.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		} else {
			try(FileWriter fw = new FileWriter(fileName, true)) {
				fw.write("ZONE DATAPACKING=POINT, I = " + N + ", VARSHARELIST=([1] = 1), SOLUTIONTIME = " + time + "\n");
				for(int i = 0; i < N; i++) {
					fw.write(y[i] + "\n");
				}
				fw.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
	
	public static void lineXYTime(String fileName, double[] x, double[][] y, double time) {
		int N = x.length;
		ParamCheck.checkEqual(N, y[0].length);
		int M = y.length;
		if(Math.abs(time) < Constants.Er) {
			try(FileWriter fw = new FileWriter(fileName)) {
				fw.write("variables = \"x\"");
				for(int i = 0; i < M; i++) {
					fw.write(", \"y\"" + i);
				}
				fw.write("\n");
				fw.write("ZONE DATAPACKING=POINT, I = " + N + ", SOLUTIONTIME = 0.0\n");
				for(int i = 0; i < N; i++) {
					fw.write(x[i] + "");
					for(int j = 0; j < M; j++) {
						fw.write("\t" + y[j][i]);
					}
					fw.write("\n");
				}
				fw.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		} else {
			try(FileWriter fw = new FileWriter(fileName, true)) {
				fw.write("ZONE DATAPACKING=POINT, I = " + N + ", VARSHARELIST=([1] = 1), SOLUTIONTIME = " + time + "\n");
				for(int i = 0; i < N; i++) {
					for(int j = 0; j < M; j++) {
						fw.write(y[j][i] + "\t");
					}
					fw.write("\n");
				}
				fw.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}		
	}
}
